
package com.hlkj.pay.service.merchant.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.framework.mybatis.core.utils.MyBatisUtils;
import com.hlkj.pay.dto.merchant.MerchantNoticeDto;
import com.hlkj.pay.dto.merchant.NoticePageQueryDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.mapper.merchant.MerchantNoticeMapper;
import com.hlkj.pay.infrastructure.mapper.merchant.NoticeMapper;
import com.hlkj.pay.infrastructure.model.merchant.MerchantNoticeDO;
import com.hlkj.pay.infrastructure.model.merchant.NoticeDO;
import com.hlkj.pay.service.CommonSnFilterService;
import com.hlkj.pay.service.merchant.INoticeService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/11/06 16:36
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class NoticeServiceImpl extends CommonSnFilterService implements INoticeService {

    private final NoticeMapper noticeMapper;

    private final MerchantNoticeMapper merchantNoticeMapper;

    @Override
    public PageResult<NoticeDO> queryPage(NoticePageQueryDto noticePageQueryDto) {
        return noticeMapper.selectPage(noticePageQueryDto, buildWrapper(noticePageQueryDto));
    }

    @Override
    public void addNotice(NoticeDO noticeDO) {
        noticeMapper.insert(noticeDO);
    }

    @Override
    public NoticeDO queryById(Long id) {
        return noticeMapper.selectById(id);
    }

    @Override
    public void updateNotice(NoticeDO noticeDO) {
        noticeMapper.updateById(noticeDO);
    }

    @Override
    public void deleteNotice(Long id) {
        noticeMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        NoticeDO noticeDO = new NoticeDO();
        noticeDO.setId(id);
        noticeDO.setStatus(status);
        noticeMapper.updateById(noticeDO);
    }

    @Override
    public void addMerchantNotice(List<MerchantNoticeDO> merchantNoticeList) {
        merchantNoticeMapper.insertBatch(merchantNoticeList);
    }

    @Override
    public MerchantNoticeDO queryMerchantNoticeById(Long id) {
        return merchantNoticeMapper.selectById(id);
    }

    @Override
    public void updateMerchantNoticeStatus(Long id, Integer code) {
        MerchantNoticeDO merchantNoticeDO = new MerchantNoticeDO();
        merchantNoticeDO.setId(id);
        merchantNoticeDO.setStatus(code);
        merchantNoticeMapper.updateById(merchantNoticeDO);
    }

    @Override
    public PageResult<MerchantNoticeDto> queryMerchantPage(NoticePageQueryDto noticePageQueryDto) {
        noticePageQueryDto.setSortingFields("a.id");
        IPage<MerchantNoticeDto> mpPage = MyBatisUtils.buildPage(noticePageQueryDto);
        QueryWrapper<MerchantNoticeDto> queryWrapper = buildMerchantWrapper(noticePageQueryDto);
        merchantNoticeMapper.selectNoticePage(mpPage, queryWrapper);
        return new PageResult(mpPage.getRecords(), mpPage.getTotal(), mpPage.getCurrent(), mpPage.getSize(), mpPage.getPages());
    }

    @Override
    public void updateSyncType(List<Long> ids, CommonEnum.YES_NO_TYPE yesNoType) {
        NoticeDO noticeDO = new NoticeDO();
        noticeDO.setNoticeType(yesNoType.getCode());
        ids.stream().forEach(id->{
            noticeDO.setId(id);
            noticeMapper.updateById(noticeDO);
        });
    }

    /**
     * 查询条件封装
     *
     * @param noticePageQueryDto
     * @return
     */
    LambdaQueryWrapper<NoticeDO> buildWrapper(NoticePageQueryDto noticePageQueryDto) {
        LambdaQueryWrapperX<NoticeDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.likeIfPresent(NoticeDO::getName, noticePageQueryDto.getLikeName());
        queryWrapper.likeIfPresent(NoticeDO::getTitle, noticePageQueryDto.getLikeTitle());
        queryWrapper.likeIfPresent(NoticeDO::getPublisher, noticePageQueryDto.getLikePublisher());
        queryWrapper.eqIfPresent(NoticeDO::getStatus, noticePageQueryDto.getStatus());
        queryWrapper.eqIfPresent(NoticeDO::getSyncType, noticePageQueryDto.getSyncType());
        queryWrapper.eqIfPresent(NoticeDO::getNoticeType, noticePageQueryDto.getNoticeType());
        queryWrapper.eqIfPresent(NoticeDO::getPublishType, noticePageQueryDto.getPublishType());
        queryWrapper.geIfPresent(NoticeDO::getEffectTime, noticePageQueryDto.getStartQueryTime());
        queryWrapper.leIfPresent(NoticeDO::getEffectTime, noticePageQueryDto.getEndQueryTime());
        return queryWrapper;
    }

    /**
     * 查询条件封装
     *
     * @param noticePageQueryDto
     * @return
     */
    QueryWrapper<MerchantNoticeDto> buildMerchantWrapper(NoticePageQueryDto noticePageQueryDto) {
        filterSn(noticePageQueryDto);
        QueryWrapper<MerchantNoticeDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("a.delete_flag", CommonEnum.YES_NO_TYPE.NO.getCode());
        queryWrapper.eq(noticePageQueryDto.getStatus() != null, "a.status", noticePageQueryDto.getStatus());
        queryWrapper.eq(StringUtils.hasText(noticePageQueryDto.getSn()), "a.sn", noticePageQueryDto.getSn());
        queryWrapper.in(!CollectionUtils.isEmpty(noticePageQueryDto.getSnList()), "a.sn", noticePageQueryDto.getSnList());
        queryWrapper.like(StringUtils.hasText(noticePageQueryDto.getName()), "info.name", noticePageQueryDto.getName());
        queryWrapper.like(StringUtils.hasText(noticePageQueryDto.getTitle()), "info.title", noticePageQueryDto.getTitle());
        queryWrapper.eq(noticePageQueryDto.getNoticeType() != null, "info.notice_type", noticePageQueryDto.getNoticeType());
        queryWrapper.eq("info.status", CommonEnum.YES_NO_TYPE.YES.getCode());
        queryWrapper.ge(noticePageQueryDto.getStartQueryTime() != null, "a.create_time", noticePageQueryDto.getStartQueryTime());
        queryWrapper.le(noticePageQueryDto.getEndQueryTime() != null, "a.create_time", noticePageQueryDto.getEndQueryTime());
        return queryWrapper;
    }
}
